/**
 * Created by Elsa on 2017/9/6.
 */

layui.define(['jquery', 'layer', 'tree'], function(exports){
    var $ = layui.jquery,
        layer = layui.layer;

    $.ajax({
        type:"GET",
        url:contextPath+"/statute",
        contentType: "application/json; charset=utf-8",
        dataType:"json",
        success:function (data) {
            console.log(data);
            layui.tree({
                elem: '#demo' //传入元素选择器
                ,click: function(item){ //点击节点回调
                    var name = encodeURI(encodeURI(item.name));
                    $('#treelist').html('<iframe frameborder=0 width="100%" height="650px" marginheight=0 marginwidth=0 scrolling=auto src=RegList.html?parentid='+item.pid+'&id='+item.id+'&name='+name+'></iframe>');
                }
                ,nodes: data
            });
        },
        error:function (e) {
        }
    });

    $.ajax({
        type:"GET",
        url:contextPath+"/personal/role",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            therole = data.role;
            if(data.role=="管理员"||data.role=="负责人"){
                var menu = new BootstrapMenu('#Menu', {
                    actions: [{
                        name: '新增节点',
                        onClick: function() {
                            var con = '<div style="padding:10px;"><input type="text" class="layui-input" style="margin-bottom:5px;" name="name" placeholder="节点名称" /></div><div align = "center"><p style="color:red">节点名称不超过30个字</p></div>';

                            layer.open({
                                type: 1,
                                title: '新增节点',
                                content: con,
                                btn: ['确定', '取消'],
                                area: ['350px', '200px'],
                                yes: function (index, layero) {
                                    //这是核心的代码。
                                    var NewNode = new Object();
                                    NewNode.id = randomChar(3);
                                    NewNode.pid = "-1";
                                    NewNode.name = $(layero).find('input[name="name"]').val();

                                    console.log(JSON.stringify(NewNode));

                                    $.ajax({
                                        type:"PUT",
                                        url:contextPath+"/statute/new",
                                        contentType: "application/json; charset=utf-8",
                                        data:JSON.stringify(NewNode),
                                        dataType:"json",
                                        success:function (data) {
                                            console.log(data);
                                            if(data == "新增成功!"){
                                                layer.msg('新增成功!<br>页面将在1秒后刷新...');
                                                setTimeout(function(){window.location.reload();},1000);
                                            } else {
                                                layer.msg(data, {
                                                    time:false,
                                                    btn:"确定",
                                                    btnAlign: 'c'
                                                });
                                            }
                                        },
                                        error:function (e) {
                                        }
                                    });


                                },
                                maxmin: false
                            });
                        }
                    }]
                });
            }else{

            }
        },
        error: function (e) {

        }
    });

    exports('RegMain', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});